import pygame
import sys
import random
# 初始化pygame
pygame.init()
# 设置屏幕大小
screen_width = 640
screen_height = 480
screen = pygame.display.set_mode((screen_width, screen_height))
#在pygame的游戏窗口中绘制文本
def draw_text(text, x, y, font_size, color):
font = pygame.font.Font(None, font_size)
        text_surface = font.render(text, True, color)
        text_rect = text_surface.get_rect()
    text_rect.center = (x, y)#中心区域的坐标
    screen.blit(text_surface, text_rect)
# 加载背景图片
background_image = pygame.image.load(r'C:\Users\lenovo\OneDrive\Pictures\Camera Roll\贪吃蛇背景3.jpg')
# 加载蛇头图片[动画表情]
snake_head_image = pygame.image.load('无标题.png')
snake_head_image.set_colorkey((255, 255, 255))#设置图像透明度的函数
#加载蛇身图片
snake_tail_image = pygame.image.load('蛇身.png')
snake_tail_image.set_colorkey((255, 255, 255))
# 设置游戏速度
clock = pygame.time.Clock()
speed = 5  # 设置帧率为10
# 定义蛇和食物的大小
block_size = 20
# 定义颜色
WHITE = (255, 255, 255)
GREEN = (0, 255, 0)
RED = (255, 0, 0)
PINK = (255, 192, 203)
# 蛇的初始位置
snake_pos = [[100, 100], [80, 100], [60, 100]]
# 食物的初始位置
food_pos = [300, 300]
# 加载背景音乐和吃食物的音效
pygame.mixer.init()
pygame.mixer.music.load("背景音乐.mp3")
pygame.mixer.music.play(-1)  # -1表示循环播放
eat_sound = pygame.mixer.Sound("音效.wav")
def draw_snake(snake_pos):
    for i, pos in enumerate(snake_pos):
        if i == 0:
            # 计算旋转角度
            if direction == 'right':
                angle = 0
            elif direction == 'left':
                angle = 180
            elif direction == 'up':
                angle = 90
            elif direction == 'down':
                angle = 270
            # 旋转蛇头图片
            rotated_head_image = pygame.transform.rotate(snake_head_image, angle)
            screen.blit(rotated_head_image, (pos[0], pos[1]))  # 使用旋转后的蛇头图片
                  else:
            screen.blit(snake_tail_image, (pos[0], pos[1]))  # 使用蛇身图片
def move_snake(snake_pos, direction):
    # 保存蛇头当前坐标
    head_x, head_y = snake_pos[0]
    # 根据方向更新蛇头坐标
    if direction == 'right':
        head_x += block_size
    elif direction == 'left':
        head_x -= block_size
    elif direction == 'up':
        head_y -= block_size
    elif direction == 'down':
        head_y += block_size
    # 将更新后的蛇头坐标插入列表开头
    snake_pos[0] = [head_x, head_y]  # 直接更新蛇头坐标，而不是插入新坐标
    # 将蛇尾的坐标复制到蛇身的下一个位置
    snake_pos.append(snake_pos[-2])
def check_collision(snake_pos, food_pos):
    if snake_pos[0] == food_pos:
        # 当蛇吃到食物时，增长蛇身
        snake_pos.append(snake_pos[-2])  # 在蛇的当前位置列表末尾添加一个新的蛇身部分
        return True
    return False
def draw_food(food_pos):
    pygame.draw.rect(screen, RED, pygame.Rect(food_pos[0], food_pos[1], block_size, block_size))
def move_snake(snake_pos, direction):
    x, y = snake_pos[0]
    if direction == 'right':
        x += block_size
    elif direction == 'left':
        x -= block_size
    elif direction == 'up':
        y -= block_size
    elif direction == 'down':
        y += block_size
    snake_pos.insert(0, [x, y])
    snake_pos.pop()
def check_collision(snake_pos, food_pos):
    if snake_pos[0] == food_pos:
        return True
    return False
def generate_food(snake_pos):
    while True:
        x = random.randrange(0, screen_width, block_size)
        y = random.randrange(0, screen_height, block_size)
        if [x, y] not in snake_pos:
            return [x, y]
def check_game_over(snake_pos):
    x, y = snake_pos[0]
    if x < 0 or x >= screen_width or y < 0 or y >= screen_height or snake_pos[0] in snake_pos[1:]:
        return True
    return False
def draw_snake(snake_pos):
    for i, pos in enumerate(snake_pos):
        if i == 0:
            # 计算旋转角度
            if direction == 'right':
                angle = 0
            elif direction == 'left':
                angle = 180
            elif direction == 'up':
                angle = 90
            elif direction == 'down':
                angle = 270
            # 旋转蛇头图片
            rotated_head_image = pygame.transform.rotate(snake_head_image, angle)
            screen.blit(rotated_head_image, (pos[0], pos[1]))  # 使用旋转后的蛇头图片
        else:
            screen.blit(snake_tail_image, (pos[0], pos[1]))  # 使用蛇身图片
direction = 'right'
while True:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            pygame.quit()
            sys.exit()
        elif event.type == pygame.KEYDOWN:
            if event.key == pygame.K_UP and direction != 'down':
                direction = 'up'
            elif event.key == pygame.K_DOWN and direction != 'up':
                direction = 'down'
            elif event.key == pygame.K_LEFT and direction != 'right':
                direction = 'left'
            elif event.key == pygame.K_RIGHT and direction != 'left':
                direction = 'right'
    move_snake(snake_pos, direction)
    if check_collision(snake_pos, food_pos):
        food_pos = generate_food(snake_pos)
        snake_pos.append(snake_pos[-1])
        eat_sound.play()  # 播放吃食物的音效
    if check_game_over(snake_pos):
        break
    screen.blit(background_image, (0, 0))  # 绘制背景图片
    draw_snake(snake_pos)
    draw_food(food_pos)
    pygame.display.flip()
    clock.tick(speed)  # 设置帧率为10
pygame.quit()
